<?php
/**
 * Created by PhpStorm.
 * User: sunku
 * Date: 2017/12/25
 * Time: 10:31
 */

namespace Resource\Service;
use Think\Model;

class CustomerService extends Model
{
    protected $trueTableName = 'wz_customers';

    /**
     * @param $params
     * @return array|bool
     */
    public function addCustomer($params) {
        $params['phase'] = 1;
        $params['create_time'] = time();
        $params['update_time'] = time();
        $id = D('Resource/Customer')->addCustomer($params);
        if($id) {
            return $id;
        } else {
            return false;
        }
    }

    /**
     * @param $params
     * @param string $fields
     * @return mixed
     */
    public function getCustomerInfo($params, $fields='') {
        $CustomerInfo = D('Resource/Customer')->getCustomerInfo($params,$fields);
        return $CustomerInfo;
    }

    /**
     * @param $params
     * @return mixed
     */
    public function getAllCustomerList($params) {
        $start = isset($params['start'])?$params['start']:'';
        $end = isset($params['end'])?$params['end']:'';
        $data = [];
        $data['status'] = ['in',[-1,0,1,5,10]];
        isset($params['admin_id']) && $data['admin_id'] =$params['admin_id'];
        isset($params['status']) && $data['status'] =$params['status'];
        if(isset($start) && $start){
            $data['create_time'] = array(array('gt',$start),array('lt',$end)) ;
        }
        $list = D('Resource/Customer')->getAllCustomerList($data);

        if(is_array($list) && count($list)>0){
            foreach($list as $k=>$v){
                $item_cate = D('Resource/ItemCate','Service')->getCateInfo(['id'=>$v['cid']]);
                $second_cate = D('Resource/ItemCate','Service')->getCateInfo(['id'=>$v['second_cid']]);
                $list[$k]['first_name'] = $item_cate['name'];
                $list[$k]['second_name'] = $second_cate['name'];

                $list[$k]['upfile_count'] = D('Resource/Upload','Service')->getUploadCount([
                    'object_id'=>$v['id'],
                    'type'=>1,
                    'status'=>1
                ]);
            }
        }

        return $list;
    }

    /**
     * @param $params
     * @return array
     */
    public function searchCustomerList($params) {
        $page = 1;
        $page_size = 15;
        $start = isset($params['start'])?$params['start']:'';
        $end = isset($params['end'])?$params['end']:'';
        isset($params['page']) && $page = $params['page'];
        isset($params['page_size']) && $page_size = $params['page_size'];

        $data = [];
        $data['status'] = ['in',[-1,0,1,5,10]];
        isset($params['company_name']) && $data['company_name'] = ['like', "%{$params['company_name']}%"];
        isset($params['type']) && $data['type'] =$params['type'];
        isset($params['phase']) && $data['phase'] =$params['phase'];
        isset($params['admin_id']) && $data['admin_id'] =$params['admin_id'];
        isset($params['admin_name']) && $data['admin_name'] =['like', "%{$params['admin_name']}%"];
        isset($params['status']) && $data['status'] =$params['status'];
        if(isset($start) && $start){
            $data['create_time'] = array(array('gt',$start),array('lt',$end)) ;
        }

        //查询总数
        $total = D('Resource/Customer')->getCustomerCount($data);

        //计算总页数
        $total_page = ceil($total / $page_size);
        if($page < 1) {
            $page = 1;
        }
        if($page > $total_page) {
            $page = $total_page;
        }

        //查询列表
        $dataList = D('Resource/Customer')->getCustomerList($data, $page, $page_size);

        $result = [];
        $result['page'] = $page;
        $result['page_size'] = $page_size;
        $result['total'] = $total;
        $result['total_page'] = $total_page;
        $result['dataList'] = $dataList;
        return $result;
    }

    /**
     * @param $params
     * @return mixed
     */
    public function updateCustomerInfo($params) {
        $params['update_time'] = time();
        return D('Resource/Customer')->saveCustomer($params);
    }
    /**
     * @param $params
     * @return array
     */
    public function searchCustomerListByCompany($params) {
        $page = 1;
        $page_size = 15;
        $start = isset($params['start'])?$params['start']:'';
        $end = isset($params['end'])?$params['end']:'';
        isset($params['page']) && $page = $params['page'];
        isset($params['page_size']) && $page_size = $params['page_size'];

        $data = [];
        isset($params['company_name']) && $data['wc.company_name'] = ['like', "%{$params['company_name']}%"];
        isset($params['type']) && $data['wc.type'] =$params['type'];
        isset($params['admin_name']) && $data['wc.admin_name'] =['like',"%{$params['admin_name']}%"];
        isset($params['product_name']) && $data['wc.product_name'] =['like',"%{$params['product_name']}%"];
        isset($params['admin_id']) && $data['wc.admin_id'] =$params['admin_id'];
        isset($params['department_id']) && $data['au.department_id'] =$params['department_id'];

        if(isset($start) && $start){
            $data['wc.create_time'] = array(array('gt',$start),array('lt',$end)) ;
        }

        //查询总数
        $total = D('Resource/Customer')->getCustomerCountByCompany($data);

        //计算总页数
        $total_page = ceil($total / $page_size);
        if($page < 1) {
            $page = 1;
        }
        if($page > $total_page) {
            $page = $total_page;
        }

        //查询列表
        $dataList = D('Resource/Customer')->getCustomerListByCompany($data, $page, $page_size);

        $result = [];
        $result['page'] = $page;
        $result['page_size'] = $page_size;
        $result['total'] = $total;
        $result['total_page'] = $total_page;
        $result['dataList'] = $dataList;
        return $result;
    }

    /**
     * 客户总数
     * @param $params
     * @return int
     */
    public function getCustomerCount($params){
        //查询总数
        $total = D('Resource/Customer')->getCustomerCount($params);
        return intval($total);
    }

    /**
     * 客户详情
     * @param $params
     * @param string $fields
     * @return mixed
     */
    public function getCustomerDetail($params, $fields='') {
        $result = D('Resource/Customer')->getCustomerInfo($params,$fields);
        if($result){
            $item_cate = D('Resource/ItemCate','Service')->getCateInfo(['id'=>$result['cid']]);
            $second_cate = D('Resource/ItemCate','Service')->getCateInfo(['id'=>$result['second_cid']]);
            $result['first_name'] = $item_cate['name'];
            $result['second_name'] = $second_cate['name'];
            $result['upfile_count'] = D('Resource/Upload','Service')->getUploadCount([
                'object_id'=>$result['id'],
                'type'=>1,
                'status'=>1
            ]);
        }
        return $result;
    }
    /**
     * @param $params
     * @return array
     */
    public function searchCommonCustomerList($params) {
        $page = 1;
        $page_size = 15;
        isset($params['page']) && $page = $params['page'];
        isset($params['page_size']) && $page_size = $params['page_size'];

        $data = [];
        $data['wc.status'] = ['in',[-1,0,1,5,10]];
        isset($params['company_name']) && $data['wc.company_name'] = ['like', "%{$params['company_name']}%"];
        isset($params['admin_name']) && $data['wc.admin_name'] = ['like', "%{$params['admin_name']}%"];
        isset($params['type']) && $data['wc.type'] =$params['type'];
        isset($params['phase']) && $data['wc.phase'] =$params['phase'];
        isset($params['admin_id']) && $data['wc.admin_id'] =$params['admin_id'];
        isset($params['status']) && $data['wc.status'] =$params['status'];
        isset($params['department_id']) && $data['ad.id'] =$params['department_id'];

        //查询总数
        $total = D('Resource/Customer')->getCommonCustomerCount($data);

        //计算总页数
        $total_page = ceil($total / $page_size);
        if($page < 1) {
            $page = 1;
        }
        if($page > $total_page) {
            $page = $total_page;
        }

        //查询列表
        $dataList = D('Resource/Customer')->getCommonCustomerList($data, $page, $page_size);

        $result = [];
        $result['page'] = $page;
        $result['page_size'] = $page_size;
        $result['total'] = $total;
        $result['total_page'] = $total_page;
        $result['dataList'] = $dataList;
        return $result;
    }
}